package com.yjd.bi.business.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.yjd.bi.business.dao.IInvestInfoDao;
import com.yjd.bi.business.model.InvestInfoModel;
import com.yjd.bi.business.service.IInvestInfoService;
import com.yjd.bi.report.enums.TimeType;
import com.yjd.bi.report.model.EChartsModel;
import com.yjd.bi.report.util.TimeDimensionUtil;
import com.yjd.comm.base.dao.IBaseDao;
import com.yjd.comm.base.service.impl.BaseServiceImpl;
import com.yjd.comm.util.ArithUtil;
import com.yjd.comm.util.DateUtil;
import com.yjd.comm.util.FrameUtil;
import com.yjd.comm.util.NumberUtil;

/**
 * <b>description</b>：投资项目业务实现<br>
 * <b>time</b>：2016-06-27 17:21:51 <br>
 * <b>author</b>：  zhoujin@yijiedai.com
 */
@Service("investInfoService")
public class InvestInfoServiceImpl extends BaseServiceImpl implements IInvestInfoService {

	/**
	 * 插入
	 * 
	 * @param model
	 * @return
	 * @throws Exception
	 */
	public InvestInfoModel insert(InvestInfoModel model) throws Exception {
		return this.investInfoDao.insertModel(model, true);
	}

	/**
	 * 更新
	 * 
	 * @param model
	 * @return
	 * @throws Exception
	 */
	public int update(InvestInfoModel model) throws Exception {
		return this.investInfoDao.updateModel(model, true);
	}

	/**
	 * 根据id删除
	 * 
	 * @param id
	 * @return
	 * @throws Exception
	 */
	public int delete(long id) throws Exception {
		Map<Object, Object> paramMap = new HashMap<Object, Object>();
		paramMap.put("id", id);
		return this.investInfoDao.deleteModel(paramMap, true);
	}

	@Resource
	private IInvestInfoDao investInfoDao;

	@Override
	public IBaseDao getBaseDao() {
		return this.investInfoDao;
	}

	@Override
	public Object getInvestPriceActual(Map<String, Object> paramMap) {
		// TODO Auto-generated method stub
		return this.investInfoDao.getInvestPriceActual(paramMap);
	}

	@Override
	public Object getInvestAmountActual(Map<String, Object> paramMap) {
		// TODO Auto-generated method stub
		return this.investInfoDao.getInvestAmountActual(paramMap);
	}

	@Override
	public Object getInvestPriceOver(Map<String, Object> paramMap) {
		// TODO Auto-generated method stub
		return this.investInfoDao.getInvestPriceOver(paramMap);
	}

	@Override
	public Object getInvestAmountOver(Map<String, Object> paramMap) {
		// TODO Auto-generated method stub
		return this.investInfoDao.getInvestAmountOver(paramMap);
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public EChartsModel getDates(TimeType timeType, Long timeFrom, String field)
			throws Exception {
		// TODO Auto-generated method stub
		EChartsModel ec = new EChartsModel();
		ec.setxAxis(FrameUtil.newArrayList(null));
		ec.setLegend(FrameUtil.newArrayList(getFormateX(timeFrom)));
		ec.addSeries(FrameUtil.newArrayList(null), getFormateX(timeFrom));
		List<List<Long>> searchParam = TimeDimensionUtil.getSearchRanges(
				timeType, timeFrom);
		for (int i = searchParam.size() - 1; i >= 0; i--) {
			ec.getXAxis().add(DateUtil.getFormateStringByTimeDimention(searchParam.get(i).get(1), "yyyy-MM-dd"));
			Double sum = ArithUtil.round(this.investInfoDao.getFundsSum(
					FrameUtil.newHashMap("field", field, "start", searchParam
							.get(i).get(0), "end", searchParam.get(i).get(1)),
					false), 2);
			ec.getSeries().get(0).getData()
					.add(NumberUtil.formatNumber(sum, NumberUtil.FORMAT_3));
		}
		return ec;
	}
	
	public String getFormateX(Long value) {
		return DateUtil.timestampToDateString(DateUtil.dateStringToTimestamp(
				value + "", DateUtil.PATTERN_yyyyMMdd),
				DateUtil.PATTERN_yyyy_MM_dd);
	}

	@Override
	public Double getrecycledPrice(Map<String, Object> paramMap) {
		// TODO Auto-generated method stub
		return this.investInfoDao.getrecycledPrice(paramMap);
	}


}
